package com.daixin.blog.controller;

import com.daixin.blog.dto.PageinationDTO;
import com.daixin.blog.model.User;
import com.daixin.blog.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * Author daixin
 */
@Controller
public class ProfileController {
    @Autowired
    private QuestionService questionService;

    /***
     * 个人中心
     * @param action
     * @param model
     * @param request
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/profile/{action}")
    public  String profile(@PathVariable(name = "action") String action, Model model, HttpServletRequest request,
                           @RequestParam(value = "page",defaultValue = "1") Integer page,
                           @RequestParam(value = "size",defaultValue =  "8") Integer  size) {
        User user =(User) request.getSession().getAttribute("user");
        if(user ==null){
            return "redirect:/";
        }
        if("questions".equals(action)){
            model.addAttribute("section", "questions");
            model.addAttribute("sectionName", "我的问题");
            PageinationDTO pageinationDTO = questionService.selectQuestionById(page, size, user.getId());
            model.addAttribute("profilePageination", pageinationDTO);
        }
        if("replies".equals(action)){
            model.addAttribute("section", "replies");
            model.addAttribute("sectionName", "我的回复");
            PageinationDTO pageinationDTO = questionService.selectQuestionById(page, size, user.getId());
            model.addAttribute("profilePageination", pageinationDTO);
        }
        return "profile";
    }
}
